<template>
    <div>
        <el-form :model="ApplyPurchase" label-width="120px" :rules="rules" ref="UpdataApplyPurchase_refs" :disabled="IsSee">
            <el-row :gutter="10">
                <el-col :span="8">
                    <el-form-item label="申购部门" prop="DepartmentId">
                        <el-select placeholder="请选择" v-model="ApplyPurchase.DepartmentId" style="width: 100%">
                            <el-option v-for="v in AllDepartment" :label="v.DepartmentName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="申购人员" prop="DepartmentId">
                        <el-select placeholder="请选择" v-model="ApplyPurchase.ApplyUserId" filterable style="width: 100%">
                            <el-option v-for="v in $store.state.AllUsers" v-if="v.InternshipState!=8" :label="v.UserName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="采购类型" prop="DepartmentId">
                        <el-select placeholder="请选择" v-model="ApplyPurchase.PurchaseType" style="width: 100%">
                            <el-option label="办公用品" :value="1"></el-option>
                            <el-option label="生产原料" :value="2"></el-option>
                            <el-option label="仪器设备" :value="4"></el-option>
                            <el-option label="其他" :value="8"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="10">
                <el-col :span="8">
                    <el-form-item label="期望交付日期" prop="DepartmentId">
                        <el-date-picker style="width: 100%" placeholder="期望交付日期"
                                        type="date"
                                        value-format="yyyy-MM-dd"
                                        v-model="ApplyPurchase.DeliveryDate"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="是否有发票" prop="DepartmentId">
                        <el-select placeholder="请选择" v-model="ApplyPurchase.IsInvoice" style="width: 100%">
                            <el-option :value="true" label="是"></el-option>
                            <el-option :value="false" label="否"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="付款方式" prop="DepartmentId">
                        <el-select placeholder="请选择" v-model="ApplyPurchase.PayMethod" style="width: 100%">
                            <el-option :value="0" label="借款"></el-option>
                            <el-option :value="1" label="对公账户"></el-option>
                            <el-option :value="2" label="电子账户"></el-option>
                            <el-option :value="4" label="备用金"></el-option>
                            <el-option :value="8" label="垫付"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="10">
                <el-col :span="8">
                    <el-form-item label="申购金额" prop="DepartmentId">
                        <el-input disabled v-model="ApplyPurchase.TotalPrice"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="垫付人" prop="DepartmentId">
                        <el-select placeholder="请选择" style="width: 100%" filterable :disabled="ApplyPurchase.PayMethod!=8||ApplyPurchase.PayMethod==''" v-model="ApplyPurchase.AdvanceParty">
                            <el-option label="无" :value="0"></el-option>
                            <el-option v-for="v in $store.state.AllUsers" v-if="v.InternshipState!=8" :label="v.UserName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="商品列表">
                        <el-table border stripe :data="tableData" ref="multipleTable">
                            <el-table-column label="名称" prop="DeviceName" align="center" show-overflow-tooltip></el-table-column>
                            <el-table-column label="参考网址" prop="ReferenceWebsite" align="center">
                                <template slot-scope="slot">
                                    <el-input v-model="slot.row.ReferenceWebsite"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="规格型号" prop="DeviceModel" align="center" show-overflow-tooltip></el-table-column>
                            <el-table-column label="申购数量" prop="Amount" align="center">
                                <template slot-scope="slot">
                                    <el-input v-model="slot.row.Amount"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="单位" prop="Unit" align="center"></el-table-column>
                            <el-table-column label="参考单价" prop="ReviewUnitPrice" align="center">
                                <template slot-scope="slot">
                                    <el-input v-model="slot.row.ReviewUnitPrice"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="参考总价" prop="TotalPrice" align="center">
                                <template slot-scope="slot">
                                    {{ Allmoney(slot.row) }}
                                </template>
                            </el-table-column>
                            <el-table-column label="到货地址" prop="ArrivalAddress" align="center">
                                <template slot-scope="slot">
                                    <el-input v-model="slot.row.ArrivalAddress"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" align="center">
                                <template slot-scope="slot">
                                    <el-button icon="el-icon-delete" size="mini" type="danger" @click="TableRemove_event(slot.row)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="用途说明" prop="ApplyPurchaseCause">
                        <el-input v-model="ApplyPurchase.ApplyPurchaseCause"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="关联项目" prop="DepartmentId">
                        <el-select v-model="ApplyPurchase.ProjectId" filterable placeholder="请选择" style="width: 100%">
                            <el-option label="无" :value="0"></el-option>
                            <el-option v-for="v in AllProject" :label="v.ProjectName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col align="center">
                    <el-button type="warning" @click="AddExistingMaterial_dialog=true">导入物资</el-button>
                    <el-button type="primary" @click="UpdateSubmit_enent">确认修改</el-button>
                    <el-button type="success" @click="Submit_enent">提交审核</el-button>
                </el-col>
            </el-row>
        </el-form>

        <el-dialog :visible.sync="AddExistingMaterial_dialog" :modal="false" width="90%" top="10px" title="现有物资导入">
            <AddExistingMaterial :selection_-material="tableData" :all-device-state="AllDeviceState" :all-device-category="AllDeviceCategory" :all-department="AllDepartment"></AddExistingMaterial>
        </el-dialog>

    </div>
</template>

<script>
    import * as publicjs from "../../../../public/Scriput/Public";
    import AddExistingMaterial from "../../Material/AddExistingMaterial";

    export default {
        name: "UpdateApplyPurchase",
        components: {
            'AddExistingMaterial': AddExistingMaterial
        },
        props: {
            IsSee: {
                type: Boolean,
                required: true,
            },
            ApplyPurchase: {
                type: Object,
                required: true,
            },
            AllDepartment: {
                type: Array,
                required: true,
            },
            AllProject: {
                type: Array,
                required: true,
            },
            AllDeviceState: {
                type: Array,
                required: true,
            },
            AllDeviceCategory: {
                type: Array,
                required: true,
            }
        },
        data() {
            return {
                //商品列表
                tableData: [],
                //物资列表弹框
                AddExistingMaterial_dialog: false,
                rules: {
                    DepartmentId: [{ required: true, message: '请选择部门', trigger: 'change' },],
                    IsInvoice: [{ required: true, message: '请选择是否有发票', trigger: 'change' },],
                    PayMethod: [{ required: true, message: '请选择付款方式', trigger: 'change' },],
                    ApplyUserId:[{ required: true, message: '请选择申购人员', trigger: 'change' },],
                    PurchaseType:[{ required: true, message: '请选择采购类型', trigger: 'change' },],
                }
            }
        },
        watch: {
            ApplyPurchase: function (obj) {
                this.init(obj)
            },
            tableData: function (obj) {
                let allmoney = 0
                obj.forEach(item => {
                    allmoney += (item.Amount * item.ReviewUnitPrice)
                })
                this.ApplyPurchase.TotalPrice = allmoney
            }
        },
        created() {
            this.init(this.ApplyPurchase)
        },
        methods: {
            //表格数据加载
            init(obj) {
                let loadding = this.openLoading();
                this.$http.get('Report/GetDetialApplyPurchaseGoods?ApplyPurchaseNumber=' + obj.ApplyPurchaseNumber).then(res => {
                    loadding.close();
                    this.tableData = res.data
                    let allmoney = 0
                    this.tableData.forEach(item => {
                        allmoney += item.TotalPrice
                    })
                    this.ApplyPurchase.TotalPrice = allmoney
                })
            },
            //表格数据移除
            TableRemove_event(obj) {
                this.$confirm('你确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'error'
                }).then(() => {
                    if (obj.Id == 0) {
                        this.tableData.forEach((item, i) => {
                            if (item.Id == obj.Id) {
                                this.tableData.splice(i, 1)
                            }
                        })
                    }
                    else {
                        let loadding = this.openLoading();
                        this.$http.post('Purchase/DeleteApplyPurchaseGoods', { Id: obj.Id }).then(res => {
                            loadding.close();
                            if (res.data.IsSuccess) {
                                this.$message.success(res.data.Msg)
                                this.tableData.forEach((item, i) => {
                                    if (item.Id == obj.Id) {
                                        this.tableData.splice(i, 1)
                                    }
                                })
                            } else {
                                this.$message.warning(res.data.Msg)
                            }
                        }).catch(() => {
                            this.$message.error('服务器异常,请联系管理员!')
                        })
                    }
                }).catch(() => {
                    this.$message.info('已取消!')
                })

            },
            //提交审核
            Submit_enent() {
                this.$refs['UpdataApplyPurchase_refs'].validate(vali=>{
                    if(vali){
                        if (this.tableData.length <= 0) {
                            this.$message.warning('物资列表不能为空')
                            return
                        }
                        for (let i = 0; i < this.tableData.length; i++) {
                            if (this.tableData[i].Amount == "0" || this.tableData[i].Price == "0") {
                                this.$message.warning('物品关键信息不能为空')
                                return;
                            }
                        }
                        this.tableData.forEach(d => {
                            d.TotalPrice = publicjs.accMul(d.Amount, d.ReviewUnitPrice).toFixed(2);
                        })
                        let loadding = this.openLoading();
                        this.$http.post('Purchase/SubmitDraftAudit', {
                            applyPurchase: this.ApplyPurchase,
                            applyPurchaseGoods: this.tableData,
                        }).then(res => {
                            loadding.close();
                            if (res.data.IsSuccess) {
                                this.$message.success('提交成功')
                                this.dialog_close()
                                this.$parent.$parent.formdata()
                                this.$parent.$parent.Update_ApplyPurchase = false
                            } else {
                                this.$message.warning('提交失败')
                            }
                        })
                    }
                })
            },
            //窗体关闭表格处理
            dialog_close() {
                // 重置表单数据
                this.$refs['UpdataApplyPurchase_refs'].resetFields()
                this.tableData = []
            },
            //确认修改
            UpdateSubmit_enent() {
                this.$refs['UpdataApplyPurchase_refs'].validate(vali=>{
                    if (vali){
                        let result = true
                        for (let i = 0; i < this.tableData.length; i++) {
                            if (this.tableData[i].Amount == "" || this.tableData[i].ReviewUnitPrice == "") {
                                result = false;
                                this.$message.warning("商品关键信息不能为空！")
                                return false;
                            }
                        }
                        if (result) {
                            let loadding = this.openLoading();
                            this.$http.post('Purchase/UpdateApplyPurchaseContent', {
                                applyPurchase: this.ApplyPurchase,
                                applyPurchaseGoods: this.tableData,
                            }).then(res => {
                                loadding.close();
                                if (res.data.IsSuccess) {
                                    this.$message.success('修改成功')
                                    this.dialog_close()
                                    this.$parent.$parent.formdata()
                                    this.$parent.$parent.Update_ApplyPurchase.dialog = false
                                } else {
                                    this.$message.warning('修改失败!')
                                }
                            })
                        }
                    }
                })
            },
            //总价
            Allmoney(row) {
                let money = 0
                this.tableData.forEach(item => {
                    money += item.Amount * item.ReviewUnitPrice
                })
                this.ApplyPurchase.TotalPrice = money.toFixed(2)
                if (isNaN(row.Amount * row.ReviewUnitPrice)) {
                    return ''
                } else {
                    return (row.Amount * row.ReviewUnitPrice).toFixed(2)
                }
            }
        },
    }
</script>

<style scoped>
</style>
